package com.tenton.servlet;

import com.tenton.pojo.Book;
import com.tenton.service.BookService;
import com.tenton.service.Impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date: 2021/1/18
 * @Author: Tenton
 * @Description: 跳转到用户修改界面
 */
@WebServlet(name = "ToUpdateBookServlet",urlPatterns = "/toUpdateBookServlet")
public class ToUpdateBookServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        BookService bookService = new BookServiceImpl();
        //获取前端传递过来的数据
        //图书Id
        String bookId = request.getParameter("id");
        int id = 0;
        //判断是否为空，然后再进行转化为int类型
        if(bookId !=null && !bookId.equals("")){
            id = Integer.parseInt(bookId);
        }
        //判断搜索字段是否为空
        if (id > 0) {
            Book book = bookService.getBook(id);
            request.setAttribute("book", book);
            request.getRequestDispatcher("/updateBook.jsp").forward(request, response);
        }
    }
}
